"use strict";

var saapsta = angular.module("saapsta", ["ajoslin.mobile-navigate"]);

saapsta.config(function ($routeProvider, $locationProvider) {
	$routeProvider.when("/", {
		redirectTo: "/empresas"
	}).when("/empresas", {
		templateUrl: "view/empresa/listar.html",
		controller: "EmpresaController"
	}).when("/empresa/:id", {
		templateUrl: "view/empresa/editar.html",
		controller: "EmpresaController"
	}).when("/empresa", {
		templateUrl: "view/empresa/adicionar.html",
		controller: "EmpresaController"
	}).when("obras/:id", {
		templateUrl: "view/obra/listar.html",
		controller: "ObraController"
	}).when("/obra/:id", {
		templateUrl: "view/obra/editar.html",
		controller: "ObraController"
	}).when("/obra", {
		templateUrl: "view/obra/adicionar.html",
		controller: "ObraController"
	});

	$locationProvider.html5Mode(false);
}).run(function ($rootScope, $route, $http, $templateCache) {
	angular.forEach($route.routes, function (r) {
		if (r.templateUrl) {
			$http.get(r.templateUrl, {
				cache: $templateCache
			});
		}
	});
}).directive("ngTap", function () {
	var isTouchDevice = !! ("ontouchstart" in window);
	return function (scope, elm, attrs) {
		if (isTouchDevice) {
			var tapping = false;
			elm.bind("touchstart", function () {
				tapping = true;
			});
			elm.bind("touchmove", function () {
				tapping = false;
			});
			elm.bind("touchend", function () {
				tapping && scope.$apply(attrs.ngTap);
			});
		} else {
			elm.bind("click", function () {
				scope.$apply(attrs.ngTap);
			});
		}
	};
});

var app = {
	initialize: function () {
		this.bindEvents();
	},
	bindEvents: function () {
		document.addEventListener("deviceready", this.onDeviceReady, true);
	},

	onDeviceReady: function () {
		angular.element(document).ready(function () {
			angular.bootstrap(document, ["saapsta"]);
		});
	},
};

app.initialize();